package tests;

import dataproviders.AscSortedData;
import dataproviders.DataProvider;
import dataproviders.DescSortedData;
import dataproviders.RandomData;
import datasorters.CountingSort;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class CountingSortTest {
    private static final Logger l = Logger.getLogger(CountingSortTest.class);

    @Before
    public void configureLogger() {
        PropertyConfigurator.configure("settings/log.properties");
    }

    @Test
    public void testNull() {
        CountingSort sorter = new CountingSort();
        sorter.sort(null);
    }

    @Test
    public void testEmpty() {
        CountingSort sorter = new CountingSort();
        sorter.sort(new Integer[0]);
    }

    @Test
    public void testAscSorted() {
        l.info("testAscSorted");
        DataProvider data = new AscSortedData();
        CountingSort sorter = new CountingSort();
        if (!validate(sorter.sort((Integer[]) data.getData(10))))
            fail("test for AscSorted failed");
        assertTrue(true);
    }

    @Test
    public void testDescSorted() {
        l.info("testDescSorted");
        DataProvider data = new DescSortedData();
        CountingSort sorter = new CountingSort();
        if (!validate(sorter.sort((Integer[]) data.getData(10))))
            fail("test for DescSorted failed");
        assertTrue(true);
    }

    @Test
    public void testRandom() {
        l.info("testRandom");
        DataProvider data = new RandomData();
        CountingSort sorter = new CountingSort();
        if (!validate(sorter.sort((Integer[]) data.getData(10))))
            fail("test for Random failed");
        assertTrue(true);
    }

    private boolean validate(Integer[] data) {
        for (int i = 0; i < data.length - 1; i++) {
            if (data[i] > data[i + 1]) {
                return false;
            }
        }
        return true;
    }
}
